package com.ayu.shortlink.admin.controller;

import com.ayu.shortlink.admin.common.convention.result.Result;
import com.ayu.shortlink.admin.common.convention.result.Results;
import com.ayu.shortlink.admin.remote.dto.ShortLinkActualRemoteService;
import com.ayu.shortlink.admin.remote.dto.req.ShortLinkBatchCreateReqDTO;
import com.ayu.shortlink.admin.remote.dto.req.ShortLinkCreateReqDTO;
import com.ayu.shortlink.admin.remote.dto.req.ShortLinkPageReqDTO;
import com.ayu.shortlink.admin.remote.dto.req.ShortLinkUpdateReqDTO;
import com.ayu.shortlink.admin.remote.dto.resp.*;
import com.ayu.shortlink.admin.util.EasyExcelWebUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 短链接后管控制层
 */
@RestController(value = "shortLinkControllerByAdmin")
@RequiredArgsConstructor
public class ShortLinkController {
    private final ShortLinkActualRemoteService shortLinkActualRemoteService;
    /**
     * 后管短链接创建
     */
    @PostMapping("/api/short-link/admin/v1/create")
    public Result<ShortLinkCreateRespDTO> createShortLink(@RequestBody ShortLinkCreateReqDTO requestParam){
         return shortLinkActualRemoteService.createShortLink(requestParam);
    }

    /**
     * 后管短链接批量创建
     */
    @PostMapping("/api/short-link/admin/v1/create/batch")
    public void batchCreateShortLink(@RequestBody ShortLinkBatchCreateReqDTO requestParam, HttpServletResponse response){
        Result<ShortLinkBatchCreateRespDTO> result = shortLinkActualRemoteService.batchCreatShortLink(requestParam);
        if(result.isSuccess()){
            List<ShortLinkBaseInfoRespDTO> baseLinkInfos = result.getData().getBaseLinkInfos();
            EasyExcelWebUtil.write(response,"AyuShortLink-批量创建短链接",ShortLinkBaseInfoRespDTO.class,baseLinkInfos);
        }

    }

    /**
     * 后管短链接查询分页
     */
    @GetMapping("/api/short-link/admin/v1/page")
    public Result<Page<ShortLinkPageRespDTO>> pageShortLink(ShortLinkPageReqDTO requestParam){
         return shortLinkActualRemoteService.pageShortLink(requestParam.getGid(),requestParam.getOrderTag(),requestParam.getCurrent(),requestParam.getSize());
    }

    /**
     * 后管更新短链接信息
     */
    @PostMapping("/api/short-link/admin/v1/update")
    public Result<Void> updateShortLink(@RequestBody ShortLinkUpdateReqDTO requestParam){
        shortLinkActualRemoteService.updateShortLink(requestParam);
        return Results.success();
    }

    /**
     * 查询短链接各分组数量
     */
    @GetMapping("/api/short-link/admin/v1/count")
    public Result<List<ShortLinkGroupCountRespDTO>> listGroupShortLinkCount(@RequestParam("requestParam") List<String> requestParam){
        return shortLinkActualRemoteService.listGroupShortLinkCount(requestParam);
    }
}
